package com.loveprogrammer.api;

import com.alibaba.fastjson.JSONObject;
import com.loveprogrammer.baidu.translate.TransApi;
import com.loveprogrammer.baidu.translate.data.Config;
import com.loveprogrammer.baidu.translate.data.Language;
import domain.TranslateResult;

import java.util.stream.Collectors;

/**
 *
 * @version 1.0.0
 * @description: 翻译文本
 * @author: eric
 * @date: 2022-04-02 14:18
 **/
public class TranslateTextApi {

    public String translate(Config config, String text,String language){
        TransApi api = new TransApi(config.getAppId(), config.getSecretKey());
        String auto = api.getTransResult(text, "auto", language);

        TranslateResult result = JSONObject.parseObject(auto, TranslateResult.class);
        return result.getTrans_result().stream().map(TranslateResult.Translate::getDst)
                .collect(Collectors.joining(""));
    }

    public static void main(String[] args) {
        Config config = new Config("20220402001155290","PCwJA_tcRO7OcxbuU47Y");

        TranslateTextApi api = new TranslateTextApi();
        System.out.println(api.translate(config, "我爱你", Language.SPA));
    }
}
